﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace NJL.WebUsers.AdminEnterprise
{
    public partial class EnterPriseService :BasePage
    {
        NJL.BLL.NJL_COMPANY_SERVICE bll = new BLL.NJL_COMPANY_SERVICE();
        NJL.Model.NJL_COMPANY_SERVICE model;
        NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        public  NJL.Model.NJL_COMPANY modelCompany = new Model.NJL_COMPANY();
        PagedDataSource pds;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["pager"] = "0";
                bind();
            }
        }
        void bind()
        {
            if (modelCompany != null)
            {
                modelCompany = bllCompany.GetCompanyByUserIDModel(UserId);                        //根据用户编号查询企业编号
                txtPage.Text = (Convert.ToInt32(ViewState["pager"]) + 1).ToString();
                pds = new PagedDataSource();
                pds.DataSource = bll.GetList("NJL_COMPANY_ID='" + modelCompany.ID+"'").Tables[0].DefaultView;
                pds.AllowPaging = true;
                pds.PageSize = 15;
                pds.CurrentPageIndex = Convert.ToInt32(ViewState["pager"]);
                ViewState["endpage"] = pds.PageCount;
                lbtnFirst.Enabled = true;
                lbtnPrev.Enabled = true;
                lbtnNext.Enabled = true;
                lbtnEnd.Enabled = true;
                if (ViewState["pager"].ToString() == "0")
                {
                    lbtnFirst.Enabled = false;
                    lbtnPrev.Enabled = false;
                }
                if (Convert.ToInt32(ViewState["pager"]) == pds.PageCount - 1)
                {
                    lbtnNext.Enabled = false;
                    lbtnEnd.Enabled = false;
                }
                lit.Text = bll.GetList("NJL_COMPANY_ID='" + modelCompany.ID+"'").Tables[0].Rows.Count.ToString();
                litAllPage.Text = pds.PageCount.ToString();
                litCurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
                rptContent.DataSource = pds;
                rptContent.DataBind();
            }
        }
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();
        }
        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) - 1;
            bind();
        }
        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) + 1;
            bind();
        }
        protected void lbtnEnd_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["endpage"]) - 1;
            bind();
        }
        private static Regex RegNumber = new Regex("^[0-9]+$");
        protected void btnRir_Click(object sender, EventArgs e)
        {
            Match mc = RegNumber.Match(txtPage.Text);
            if (mc.Success && txtPage.Text != "0")
            {
                ViewState["pager"] = Convert.ToInt32(txtPage.Text) - 1;
                bind();
            }
        }
        protected void rptContent_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                model = bll.GetModel(id);
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                }
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
    }
}